package me.leiusual.sharing.ui.cart

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import com.google.gson.JsonObject
import me.leiusual.sharing.api.model.OrderCheckout
import me.leiusual.sharing.api.model.orderconfirm.OrderConfirm
import me.leiusual.sharing.repository.OrderRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.base.AbstractViewModel
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-28
 * description:
 */
class CartOrderViewModel @Inject constructor(
    private val orderRepository: OrderRepository
) : AbstractViewModel() {

  // 订单确认

  private val _orderConfirm = MutableLiveData<Map<Long, Int>>()
  val orderConfirm: LiveData<Data<OrderConfirm>> = switchMap(_orderConfirm) {
    orderRepository.orderConfirm(it).toLiveData()
  }

  fun orderConfirm(map: Map<Long, Int>) {
    _orderConfirm.value = map
  }

  // 订单生成

  private val _orderCheckout = MutableLiveData<OrderCheckout.Request>()
  val orderCheckoutParams: LiveData<OrderCheckout.Request> = _orderCheckout
  val orderCheckout: LiveData<Data<JsonObject>> = switchMap(_orderCheckout) {
    orderRepository.orderCheckout(it).toLiveData()
  }

  fun orderCheckout(request: OrderCheckout.Request) {
    _orderCheckout.value = request
  }
}